2

之前换电脑装了个Mnmp,有遇到一些小坑,写在这,希望能帮到一些初次搭建Mnmp的phper。

.
.
.

安装 Mac 的包管理器 - homebrew

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。

安装Homebrew之前,需要确定mac是否安装过xcode,然后安装xcode命令行工具。

#安装xcode命令行工具
xcode-select --install

如果该方法你不愿用或者各种原因,可以:

登录 [https://developer.apple.com/download/more/][1] 然后下载 dmg 安装

注:一定要选择和mac系统版本,xcode版本一致的命令行工具。

好了现在我们开始安装Homebrew。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装好了Homebrew之后,便可以使用brew命令来安装相应的包了。

Nginx

brew install nginx

执行完之后Nginx就安装好了,以下为nginx几个常用命令。

# 启动 nginx服务
sudo nginx

# 重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit

#测试配置是否有语法错误
nginx -t
# 启动 nginx
sudo ngixn -c /usr/local/etc/nginx/nginx.conf

#测试配置是否有语法错误
nginx -t -c /usr/local/etc/nginx/nginx.conf

#nginx 配置php
location ~ \.php$ {
    root           /usr/share/nginx/;#此处html改为目录
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

nginx启动后,在浏览器中输入http://localhost:8080/,回车即可看到运行结果,显示的是/usr/local/Cellar/nginx/1.10.0/html/index.html文件的内容。

设置开机自启动nginx服务设置:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/nginx/1.10.0/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

MySQL

brew install mysql
//执行
mysql_secure_installation
//如果报如下错误:
//ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

//执行
sudo chmod 777 /var/run/mysql

//再次执行
mysql_secure_installation


//开始对mysql进行安全设置
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
//是否设置验证密码组件:y
Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
//密码验证策略有三个级别:0=低,1=中,2=强:0
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
//输入密码:admin123
New password: 
//重复密码:admin123
Re-enter new password: 

Estimated strength of the password: 50 
//密码强度50,是否使用这个密码:y
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
//删除匿名用户(测试用用户):y
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
//不允许远程登录根目录:n   (此处根据个人选择,建议输入y)
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

//是否删除测试数据库:y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

//是否刷新权限:y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

//完成
All done! 

// nginx启停

server.mysql start
             stop
             restart

#登录mysql
mysql -u root -p

设置开机启动

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/Cellar/mysql/5.7.12/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

PHP

brew install php@7.1
sudo vim ~/.bash_profile 
export PATH="$(brew --prefix php@7.1)/bin:$PATH"

sudo php-fpm

ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)
#解决
cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf 
ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
#解决
修改 php-fpm.conf error_log 配置为 /usr/local/var/log/php-fpm.log

php-fpm启动报错No pool defined failed to post process the configuration FPM initialization failed
#解决:
cd /private/etc/php-fpm.d/
sudo cp www.conf.default www.conf
# 重新启动php-fpm即可

ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
#解决
编辑 php-fpm.conf / /private/etc/php-fpm.d/www.conf,修改 listen 为 127.0.0.1:9999

php设置开机启动

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

开启:
brew services start php@7.2
重启:
brew services restart php@7.2
停止:
brew services stop php@7.2

配置 Nginx

vim /usr/local/etc/nginx/nginx.conf

#隐藏入口文件配置
location / {
   index  index.php index.html index.htm;
   if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?$1 last;    #ci框架写法
        #rewrite ^/(.*)$ /index.php?s=/$1 last;    #tp框架写法
        break;
   }
}

修改host

vim /etc/hosts
# 127.0.0.1 www.test.com #

王不凡
52 声望0 粉丝

程序员最讨厌的四件事: